<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>人事管理系统——修改用户</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"/>
	<meta http-equiv="description" content="This is my page"/>
	<link href="../css/css.css" type="text/css" rel="stylesheet"/>
	<link rel="stylesheet" type="text/css" href="../js/ligerUI/skins/Aqua/css/ligerui-dialog.css"/>
	<link href="../js/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css"/>
	<script type="text/javascript" src="../js/jquery-1.11.0.js"></script>
	<script type="text/javascript" src="../js/jquery-migrate-1.2.1.js"></script>
	<script src="../js/ligerUI/js/core/base.js" type="text/javascript"></script>
	<script src="../js/ligerUI/js/plugins/ligerDrag.js" type="text/javascript"></script>
	<script src="../js/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
	<script src="../js/ligerUI/js/plugins/ligerResizable.js" type="text/javascript"></script>
	<link href="../css/pager.css" type="text/css" rel="stylesheet"/>
	<script type="text/javascript">


	</script>
</head>
<body>
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td height="10"></td>
		</tr>
		<tr>
			<td width="15" height="32"><img src="../images/main_locleft.gif" width="15" height="32"></td>
			<td class="main_locbg font2"><img src="../images/pointer.gif">&nbsp;&nbsp;&nbsp;当前位置：用户管理 &gt; 修改用户</td>
			<td width="15" height="32"><img src="../images/main_locright.gif" width="15" height="32"></td>
		</tr>
	</table>
	<table width="100%" height="90%" border="0" cellpadding="5" cellspacing="0" class="main_tabbor">
		<tr valign="top">
			<td>
				<!-- 隐藏表单，flag表示添加标记 -->
				<input type="hidden" name="flag" value="2">
				<input type="hidden" name="id" value="">
				<table width="100%" border="0" cellpadding="0" cellspacing="10" class="main_tab">
					<tr>
						<td class="font3 fftd">
							<table>
								<tr>
									<td class="font3 fftd">状&nbsp;&nbsp;&nbsp;&nbsp;态：
										<select name="status" id="status">
											<option value="1">开启</option>
											<option value="2">关闭</option>
										</select>
									</td>
								</tr>
								<tr>
									<td class="font3 fftd">用户名：
										<input type="text" name="username" id="username" size="20" value=""/>
									</td>
								</tr>
								<tr>
									<td class="font3 fftd">密&nbsp;&nbsp;&nbsp;&nbsp;码：
										<input type="password" name="password" id="password" size="20" value=""/>
									</td>
								</tr>
								<tr>
									<td class="font3 fftd">邮&nbsp;&nbsp;&nbsp;&nbsp;箱：
										<input type="text" name="email" id="email" size="20" value=""/>
									</td>
								</tr>
								<tr>
									<td class="font3 fftd">电&nbsp;&nbsp;&nbsp;&nbsp;话：
										<input type="number" name="cellphone" id="cellphone" size="11" value=""/>
									</td>
								</tr>
								<tr>
									<td class="font3 fftd">性&nbsp;&nbsp;&nbsp;&nbsp;别：
										<span>男</span>&nbsp;&nbsp;<input type="radio" name="gender" id="gender1"
										                                 value="1"/>
										&nbsp;&nbsp;
										<span>女</span>&nbsp;&nbsp;<input type="radio" name="gender" id="gender2"
										                                 value="2"/>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td class="main_tdbor"></td>
					</tr>
					<tr>
						<td align="left" class="fftd"><input type="button" onclick="submit()"
						                                     value="修改 ">&nbsp;&nbsp;<input onclick="reset()"
						                                                                    type="reset" value="取消 ">
						</td>
					</tr>
				</table>
				<div style="height:10px;"></div>
			</td>
		</tr>
	</table>
	<script type="text/javascript">
		function checkResultAndGetData($result) {
			if ($result.success == false) {
				throw $result;
			}
			return $result.data;
		}

		function reset() {
			window.location.replace('/csi/user/user.html');
		}

		function submit() {

			/** 员工表单提交 */
			let username = $("#username").val();
			let status = $("#status").val();
			let email = $("#email").val();
			let cellphone = $("#cellphone").val();
			let gender = $("#gender").val();

			let userInfo = {
				"id": userId,
				"username": username,
				"status": status,
				"email": email,
				"cellphone": cellphone,
				"gender": gender
			};
			let msg = "";
			if ($.trim(username) == "") {
				msg = "登录名不能为空！";
			} else if ($.trim(username).length < 6 || $.trim(username).length > 20) {
				msg = "登录名需要大于6位小于20位！";
			}
			if (msg != "") {
				$.ligerDialog.error(msg);
				return false;
			}
			console.log(JSON.stringify(userInfo))
			$.ajax({
				type: "PUT",
				url: "api/update",
				async: true,
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify(userInfo),
				dataType: "json",
				error: function (XMLHttpRequest, textStatus, text) {
					$.ligerDialog.error(JSON.stringify(text), "出错了，请于管理员联系");
				},
				success: function (result) {
					console.log(result);
					let data;
					try {
						data = checkResultAndGetData(result);
					} catch (error) {
						console.log(error)
						// alert(JSON.stringify(error));
						$.ligerDialog.error(JSON.stringify(error), "出错了，请于管理员联系");
						return;
					}
					if (data) {
						$.ligerDialog.success("修改成功");
					} else {
						$.ligerDialog.warn("修改失败");
					}
				}
			});
		}

		let userId = null;
		$(document).ready(function () {
			// 获取url上的site参数并且传入表单
			let searchParams = new URLSearchParams(window.location.search);
			console.log(searchParams);
			userId = searchParams.get("userId");
			$.ajax({
				url: "api/get/" + userId, // url where to submit the request
				type: "GET", // type of action POST || GET
				success: function (result) {
					// console.log(result);
					let data;
					try {
						data = checkResultAndGetData(result);
					} catch (error) {
						console.log(error)
						alert(JSON.stringify(error));
						return;
					}

					console.log(data);
					if (data == null) {
						return;
					}
					$("#email").attr("value", data.email);
					if (data.gender == 1) {
						$("#gender1").attr("checked", "checked");
					} else {
						$("#gender2").attr("checked", "checked");
					}
					$("#username").attr("value", data.username);
					$("#password").attr("value", data.password);
					$("#cellphone").attr("value", data.cellphone);
				}
			});
		});
	</script>
</body>
</html>